package zhexian.app.smartcall.contact;


import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.syw.avatar.CropperActivity;
import com.syw.avatar.util.SLog;

import java.util.HashMap;

import zhexian.app.smartcall.MainActivity;
import zhexian.app.smartcall.R;
import zhexian.app.smartcall.base.BaseApplication;
import zhexian.app.smartcall.base.Const;
import zhexian.app.smartcall.tools.UploadUtil;


public class SlidingMenuFragment extends Fragment implements View.OnClickListener, PopupMenu.OnMenuItemClickListener, Handler.Callback {

    private static final String TAG = SlidingMenuFragment.class.getSimpleName();

    BaseApplication baseApp;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_sliding_menu, container, false);

        baseApp = (BaseApplication)(getActivity()).getApplication();
        bindListener(view);

        return view;
    }

    private void bindListener(View rootView) {
        Button btnSetAvatar = (Button) rootView.findViewById(R.id.menu_set_avatar);
        Button btnLogout = (Button) rootView.findViewById(R.id.menu_logout);

        btnSetAvatar.setOnClickListener(this);
        btnLogout.setOnClickListener(this);

        UploadUtil uploadUtil = UploadUtil.getInstance();
        uploadUtil.setOnUploadProcessListener(CachedUploadListener.getInstance());  //设置监听器监听上传状态



    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.menu_set_avatar:
                PopupMenu pop = new PopupMenu(getActivity(), view);
                pop.getMenuInflater().inflate(R.menu.menu_take_avatar, pop.getMenu());
                pop.setOnMenuItemClickListener(this);
                pop.show();
                break;
            case R.id.menu_logout:
                MainActivity mainActivity = (MainActivity)getActivity();
                mainActivity.baseApp.setIsLogin(false);
                mainActivity.JumpToLogin();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem menuItem) {
        Intent intent;
        switch (menuItem.getItemId()) {
            case R.id.menu_take_photo:
                intent = new Intent(getActivity(), CropperActivity.class);
                intent.putExtra("PickWay", "TAKE");
                startActivityForResult(intent, 10002);
                break;
            case R.id.menu_pick_photo:
                intent = new Intent(getActivity(), CropperActivity.class);
                intent.putExtra("PickWay", "PICK");
                startActivityForResult(intent, 10001);
                break;
            default:
                break;
        }
        return false;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_CANCELED){
            SLog.d(TAG, "Canceled Avatar Capture.");
            return;
        }
        // 根据上面发送过去的请求码来区别
        switch (requestCode) {
            case 10001:
            case 10002:
                final String path = data.getStringExtra("CropperPhotoPath");
                SLog.d(TAG, "CropperPhotoPath = " + path);
                if (path != null && path.length() > 0) {
                    uploadAvatar(path);
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onStart() {
        Log.i(TAG, "SlidingMenuFragment started");
        CachedUploadListener.getInstance().setUploadMessageHandler(this);
        super.onStart();
    }

    @Override
    public void onStop() {
        Log.i(TAG, "SlidingMenuFragment stopped");
        // 解除引用，避免Activity对象泄露
        CachedUploadListener.getInstance().setUploadMessageHandler(null);
        super.onStop();
    }

    private void uploadAvatar(String filePath) {
        String uploadUrl = this.baseApp.getServiceUrl(Const.URL_UPLOAD_AVATAR);
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("username", this.baseApp.getUserName());
        params.put("password", this.baseApp.getPassword());
        UploadUtil.getInstance().uploadFile(filePath, "avatar", uploadUrl, params);
    }

    @Override
    public boolean handleMessage(Message message) {
        Log.d(TAG, "接收到上传消息, message.what=" + message.what);
        ((MainActivity)getActivity()).getHandler().sendMessage(message);
        return true;
    }
}